package ilvonen.ihstatistics.repository;

import ilvonen.ihstatistics.models.Stat;
import ilvonen.ihstatistics.models.Team;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

public class DBStats {
	//define table name
		private static final String TABLE_NAME = "stats";
	//define tables column names
		private static final String KEY_ID = "_id";
		private static final String KEY_ROSTER_ID = "roster_id";
		private static final String KEY_GAME_ID = "game_id";
		private static final String KEY_STAT_TYPE_ID = "stat_type_id";
		private static final String KEY_VALUE = "value";
		private static final String KEY_TIME = "time";
		private static final String KEY_COORDS = "coordinates";
		private static final String KEY_PENALTY = "penalty_reason_id";
		private static final String KEY_ASSIST_1 = "assistant_1";
		private static final String KEY_ASSIST_2 = "assistant_2";
	//define create statement for all your tables. Create new file (class) for each table.
		private static final String CREATE_STATEMENT = "create table if not exists " + TABLE_NAME + " (" 
														+ KEY_ID + " integer primary key autoincrement, "
														+ KEY_ROSTER_ID + " integer not null, "
														+ KEY_GAME_ID + " integer not null, "
														+ KEY_STAT_TYPE_ID + " integer not null, "
														+ KEY_VALUE + " integer not null, "
														+ KEY_TIME + " text not null, "
														+ KEY_COORDS + " text, "
														+ KEY_PENALTY + " integer, "
														+ KEY_ASSIST_1 + " integer, "
														+ KEY_ASSIST_2 + " integer);";
	
	/*
	 * CLASS STATUS: READY:YES
	 */
	public DBStats(){}
	/*
	 * DONE
	 */
	public String create(){
		return CREATE_STATEMENT;
	}
	/*
	 * DONE
	 */
	public long insert(int playerAndGame, int gameId, int statType, int value, String time, String coordinates, int penaltyId, int assist1, int assist2, DBAdapter db){
		
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_ROSTER_ID, playerAndGame);
		initialValues.put(KEY_GAME_ID, gameId);
		initialValues.put(KEY_STAT_TYPE_ID, statType);
		initialValues.put(KEY_VALUE, value);
		initialValues.put(KEY_TIME, time);
		initialValues.put(KEY_COORDS, coordinates);
		initialValues.put(KEY_PENALTY, penaltyId);
		initialValues.put(KEY_ASSIST_1, assist1);
		initialValues.put(KEY_ASSIST_2, assist2);
		
		return db.insertRecord(TABLE_NAME, initialValues);
		
	}
	/*
	 * DONE
	 */
	public boolean delete(long rowId, DBAdapter db){
		return db.deleteRecord(TABLE_NAME, KEY_ID, rowId);
	}
	/*
	 * DONE
	 */
	public List<Stat> getAllStats(DBAdapter db){
		Cursor c;
		List<Stat> stats = new ArrayList<Stat>();
		c = db.getAllRecords(TABLE_NAME, new String[]{KEY_ID,KEY_ROSTER_ID,KEY_GAME_ID,KEY_STAT_TYPE_ID,KEY_VALUE,KEY_TIME,KEY_COORDS,KEY_PENALTY,KEY_ASSIST_1,KEY_ASSIST_2});
		while(c.moveToNext()){
			Stat s = new Stat();
			s.set_id(c.getInt(0));
			s.set_playerInGameId(c.getInt(1));
			s.set_gameId(c.getInt(2));
			s.set_statTypeId(c.getInt(3));
			s.set_value(c.getInt(4));
			s.set_time(c.getString(5));
			s.set_coordinates(c.getString(6));
			s.set_penaltyReason(c.getInt(7));
			s.set_assistant1(c.getInt(8));
			s.set_assistant2(c.getInt(9));
			stats.add(s);
		}
		return stats;
	}
	/*
	 * DONE
	 */
	public List<Stat> getAllStatsByGameId(DBAdapter db, long ROW_ID){
		Cursor c;
		List<Stat> stats = new ArrayList<Stat>();
		c = db.getAllRecordsById(TABLE_NAME, new String[]{KEY_ID,KEY_ROSTER_ID,KEY_GAME_ID,KEY_STAT_TYPE_ID,KEY_VALUE,KEY_TIME,KEY_COORDS,KEY_PENALTY,KEY_ASSIST_1,KEY_ASSIST_2}, KEY_GAME_ID, ROW_ID);
		while(c.moveToNext()){
			Stat s = new Stat();
			s.set_id(c.getInt(0));
			s.set_playerInGameId(c.getInt(1));
			s.set_gameId(c.getInt(2));
			s.set_statTypeId(c.getInt(3));
			s.set_value(c.getInt(4));
			s.set_time(c.getString(5));
			s.set_coordinates(c.getString(6));
			s.set_penaltyReason(c.getInt(7));
			s.set_assistant1(c.getInt(8));
			s.set_assistant2(c.getInt(9));
			stats.add(s);
		}
		return stats;
	}
	/*
	 * DONE
	 */
	public List<Stat> getAllStatsByPIGID(DBAdapter db, long ROW_ID){
		Cursor c;
		List<Stat> stats = new ArrayList<Stat>();
		c = db.getAllRecordsById(TABLE_NAME, new String[]{KEY_ID,KEY_ROSTER_ID,KEY_GAME_ID,KEY_STAT_TYPE_ID,KEY_VALUE,KEY_TIME,KEY_COORDS,KEY_PENALTY,KEY_ASSIST_1,KEY_ASSIST_2}, KEY_ROSTER_ID, ROW_ID);
		while(c.moveToNext()){
			Stat s = new Stat();
			s.set_id(c.getInt(0));
			s.set_playerInGameId(c.getInt(1));
			s.set_gameId(c.getInt(2));
			s.set_statTypeId(c.getInt(3));
			s.set_value(c.getInt(4));
			s.set_time(c.getString(5));
			s.set_coordinates(c.getString(6));
			s.set_penaltyReason(c.getInt(7));
			s.set_assistant1(c.getInt(8));
			s.set_assistant2(c.getInt(9));
			stats.add(s);
		}
		return stats;
	}
	/*
	 * DONE
	 */
	public List<Stat> getAllStatsByStatTypeId(DBAdapter db, long ROW_ID){
		Cursor c;
		List<Stat> stats = new ArrayList<Stat>();
		c = db.getAllRecordsById(TABLE_NAME, new String[]{KEY_ID,KEY_ROSTER_ID,KEY_GAME_ID,KEY_STAT_TYPE_ID,KEY_VALUE,KEY_TIME,KEY_COORDS,KEY_PENALTY,KEY_ASSIST_1,KEY_ASSIST_2}, KEY_STAT_TYPE_ID, ROW_ID);
		while(c.moveToNext()){
			Stat s = new Stat();
			s.set_id(c.getInt(0));
			s.set_playerInGameId(c.getInt(1));
			s.set_gameId(c.getInt(2));
			s.set_statTypeId(c.getInt(3));
			s.set_value(c.getInt(4));
			s.set_time(c.getString(5));
			s.set_coordinates(c.getString(6));
			s.set_penaltyReason(c.getInt(7));
			s.set_assistant1(c.getInt(8));
			s.set_assistant2(c.getInt(9));
			stats.add(s);
		}
		return stats;
	}
	/*
	 * DONE
	 */
	public Stat getStatByStatId(DBAdapter db, long ROW_ID){
		Stat s = null;
		Cursor c;
		c = db.getRecord(TABLE_NAME, new String[]{KEY_ID,KEY_ROSTER_ID,KEY_GAME_ID,KEY_STAT_TYPE_ID,KEY_VALUE,KEY_TIME,KEY_COORDS,KEY_PENALTY,KEY_ASSIST_1,KEY_ASSIST_2}, KEY_ID, ROW_ID);
		if(c != null){
			s = new Stat();
			s.set_id(c.getInt(0));
			s.set_playerInGameId(c.getInt(1));
			s.set_gameId(c.getInt(2));
			s.set_statTypeId(c.getInt(3));
			s.set_value(c.getInt(4));
			s.set_time(c.getString(5));
			s.set_coordinates(c.getString(6));
			s.set_penaltyReason(c.getInt(7));
			s.set_assistant1(c.getInt(8));
			s.set_assistant2(c.getInt(9));
		}
		return s;
	}

}
